package cn.exrick.front.controller;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbAdvice;
import cn.exrick.manager.pojo.TbMember;
import cn.exrick.manager.service.AdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class AdviceController {

    @Autowired
    private AdviceService adviceService;

    @PostMapping("/advice/add")
    public Result<Object> addAdvice(HttpSession session,String advice){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        TbAdvice tbAdvice = new TbAdvice();
        tbAdvice.setAdvice(advice);
        tbAdvice.setCreateBy(member.getId());
        tbAdvice.setCreateTime(new Date());
        tbAdvice.setUpdateBy(member.getId());
        tbAdvice.setUpdateTime(new Date());
        adviceService.addAdvice(tbAdvice);
        return new ResultUtil<>().setData(null);
    }

    @PostMapping("/advice/del")
    public Result<Object> delAdvice(Long id){
        adviceService.delAdvice(id);
        return new ResultUtil<>().setData(null);
    }

    @PostMapping("/advice/list")
    public List<TbAdvice> getAdviceList(HttpSession session){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        return adviceService.getAdviceByMemeberId(member.getId());
    }

}
